package com.ibp.service.impl;  																		  

import java.util.HashMap;
import java.util.List;																					
import java.util.Map;

import org.slf4j.Logger;																				
import org.slf4j.LoggerFactory;																			
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;											
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;															
import org.springframework.web.context.ServletContextAware;

import com.ibp.dao.DictDataMapper;																		
import com.ibp.entity.DictData;																	
import com.ibp.service.DictDataService;																	
import com.github.pagehelper.PageInfo;																	

@Lazy(false)		
@Service("dictDataServiceImpl")																	
public class DictDataServiceImpl extends BasicServiceImpl<DictData>  implements DictDataService,ServletContextAware,InitializingBean{	
		
	    private static Logger logger = LoggerFactory.getLogger(DictDataServiceImpl.class);				  

		@Autowired																							
		DictDataMapper dictDataMapper ;	
		
		private Map<String,Map<String,String>> dictMap ;
        
		public List<DictData> getList() {									
			logger.info("查询字典全部列表");																		
			List<DictData> u = dictDataMapper.getList(new DictData());								
			return u;																						
		}			
		
		
		@Override																							
		public List<DictData> getList(DictData dictData) {									
			logger.info("查询字典列表");																		
			List<DictData> u = dictDataMapper.getList(dictData);								
			return u;																						
		}																								  

		@Override																							
		public DictData get(DictData dictData) {											
			DictData u = dictDataMapper.get(dictData);											
			return u;																						
		}																								  

		@Override																							
		public void add(DictData dictData) {														
			dictDataMapper.add(dictData);															
		}																								  

		@Override																							
		public void update(DictData dictData) {													
			dictDataMapper.update(dictData);															
		}																								  

		@Override																							
		public void delete(DictData dictData) {													
			dictDataMapper.delete(dictData);															
		}																								  

		@Override																							
		public PageInfo<DictData> getPage(DictData dictData) {								
			return null;		
		}

		@Override
		public void afterPropertiesSet() throws Exception {
			logger.info("缓存字典数据进内存...");
			//查询全部的字典数据
			List<DictData> list = this.getList() ;
			dictMap = new HashMap<String,Map<String,String>>();
			if(list!=null && list.size()>0){
				for(DictData data : list){
					Map<String,String> map = dictMap.get(data.getTypeCode());
					if(map==null){
						map = new HashMap<String,String>();
						dictMap.put(data.getTypeCode(), map);
					}
					map.put(data.getCode(), data.getName());
				}
			}
			
			super.servletContext.setAttribute(DictData.CONTEXT_ATTR_KEY, dictMap);
		}																									
}																									  


